package com.quplayer.qubrowser;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;

import com.quplayer.qubrowser.fragments.HomeFragment;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity  implements OnTabSelectListener {
    @BindView(R.id.layout_container)
    FrameLayout layoutContainer;

    @BindView(R.id.bottom_bar)
    BottomBar bottomBar;


    private HomeFragment mHomeFragment;

    // 当前正在显示的Fragment
    private Fragment mCurrentFragment;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initData(savedInstanceState);
    }

    public void initData(@Nullable Bundle savedInstanceState) {
        retrieveFragments();
        bottomBar.setOnTabSelectListener(this);
    }
    // 找回FragmentManager中存储的Fragment
    private void retrieveFragments() {
        FragmentManager manager = getSupportFragmentManager();
        mHomeFragment = (HomeFragment) manager.findFragmentByTag(HomeFragment.class.getName());

    }
    @Override
    public void onBackPressed() {
        if (mCurrentFragment != mHomeFragment) {
            // 如果不是在HomeFragment, 则按返回键回到HomeFragment
            bottomBar.selectTabWithId(R.id.tab_home);
            return;
        }

        // 将Activity所在的Task移到后台, 而不是finish此Activity
        moveTaskToBack(true);
    }
    @Override
    public void onTabSelected(int tabId) {
        switch (tabId) {
            case R.id.tab_home:
                if (mHomeFragment == null) mHomeFragment = HomeFragment.newInstance();
                switchFragment(mHomeFragment);
                break;
            /*case R.id.tab_category:
                if (mCategoryFragment == null) mCategoryFragment = VideoFragment.newInstance();
                switchFragment(mCategoryFragment);
                break;

            case R.id.tab_mine:
                if (mMineFragment == null) mMineFragment = MineFragment.newInstance();
                switchFragment(mMineFragment);
                break;*/
            default:
                throw new UnsupportedOperationException("Illegal branch!");
        }
    }
    /**
     * 首页Fragment切换, 使用hide和show, 而不是replace.
     *
     * @param target 要显示的目标Fragment.
     */
    private void switchFragment(Fragment target) {
        if (mCurrentFragment == target) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mCurrentFragment != null) {
            // 隐藏当前正在显示的Fragment
            transaction.hide(mCurrentFragment);
        }
        if (target.isAdded()) {
            // 如果目标Fragment已经添加过, 就显示它
            transaction.show(target);
        } else {
            // 否则直接添加该Fragment
            transaction.add(R.id.layout_container, target, target.getClass().getName());
        }
        transaction.commit();
        mCurrentFragment = target;
    }

}
